package com.yuanfeng.userms.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.PageUserTokenBeanDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.util.ParseMapUtils;
import com.yuanfeng.commoms.util.ParseTokenUtils;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.commoms.util.excel.ExcelsUtil;
import com.yuanfeng.commoms.util.querypage.PageUtils;
import com.yuanfeng.commoms.util.querypage.Query;
import com.yuanfeng.commoms.vo.user.UserInfoVO;
import com.yuanfeng.userms.entity.UserPlusOrderEntity;
import com.yuanfeng.userms.mapper.UserPlusOrderMapper;
import com.yuanfeng.userms.service.UserPlusOrderService;
import com.yuanfeng.userms.vo.PlusOrderEntityExpVO;
import com.yuanfeng.userms.vo.PlusOrderEntityVO;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.TemporalAdjusters;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName UserPlusOrderServiceImpl
 * @Description
 * @Author cyp
 * @Date 2023-07-04 09:36:32
 * @Version 1.0
 **/
@Service
public class UserPlusOrderServiceImpl extends ServiceImpl<UserPlusOrderMapper, UserPlusOrderEntity>
        implements UserPlusOrderService {

    @Override
    public ResponseResult get(PageUserTokenBeanDTO pageUserTokenBeanDTO) {
        Map<String, Object> hashMap = new HashMap<>();

        UserInfoVO userInfo = ParseTokenUtils.getPlatformUser(pageUserTokenBeanDTO.getToken());

        if (null == userInfo ) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID.getCode(),BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        }

        try {
            pageUserTokenBeanDTO.setUserId(userInfo.getUserId());
            //查询会员充值列表
            Map data = ParseMapUtils.beanToMap(pageUserTokenBeanDTO);
            //分页查询数据
            IPage<PlusOrderEntityVO> page = this.baseMapper.selectplusorder(new Query<PlusOrderEntityVO>().getPage(data),pageUserTokenBeanDTO);

            return ResponseResult.success(PageUtils.getPage(page));
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return ResponseResult.fail(BizCodeEnume.UNKNOW_EXCEPTION.getCode(),BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
    }
    @Override
    public ResponseResult exp(Map<String, Object> map) {
        Map<String, Object> hashMap = new HashMap<>();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + map.get("token"));
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }
        if (map.get("startTime") == ""){
            map.put("startTime",null);
        }
        if (map.get("endTime") == ""){
            map.put("endTime",null);
        }
        String[] title = {"会员ID", "会员名称", "会员手机号", "套餐类型", "本次开通金额", "充值方式", "本次生效时间", "本次到期时间","支付时间","充值单号"};
        List<Map<String, String>> list = Lists.newArrayList();
        //查询会员充值列表
        IPage<PlusOrderEntityExpVO> page=this.baseMapper.selectrechargelistexp(new Query<PlusOrderEntityExpVO>().getPage(map),
                map);

        for (PlusOrderEntityExpVO plusOrderEntity : page.getRecords()) {
            if (plusOrderEntity.getThaliType() == 1){
                plusOrderEntity.setThaliTypes("月卡");
            }else if (plusOrderEntity.getThaliType() == 2){
                plusOrderEntity.setThaliTypes("季卡");
            }else if (plusOrderEntity.getThaliType() == 3){
                plusOrderEntity.setThaliTypes("年卡");
            }

            //支付方式 1-余额支付，2-购物卡，3-微信支付，4 支付宝 5 平台赠送
            if (plusOrderEntity.getPayType() == 1){
                plusOrderEntity.setPayTypes("余额支付");
            }else if(plusOrderEntity.getPayType() == 2){
                plusOrderEntity.setPayTypes("购物卡");
            }else if(plusOrderEntity.getPayType() == 3){
                plusOrderEntity.setPayTypes("微信支付");
            }else if(plusOrderEntity.getPayType() == 4){
                plusOrderEntity.setPayTypes("支付宝");
            }else if(plusOrderEntity.getPayType() == 5){
                plusOrderEntity.setPayTypes("平台赠送");
            }


            HashMap<String, String> map1 = com.google.common.collect.Maps.newHashMap();
            map1.put("会员ID", plusOrderEntity.getUserId() + "");
            map1.put("会员名称", plusOrderEntity.getUserName() + "");
            map1.put("会员手机号", plusOrderEntity.getUserMobile() + "");
            map1.put("套餐类型", plusOrderEntity.getThaliTypes() + "");
            map1.put("本次开通金额", plusOrderEntity.getPayPrice() + "");
            map1.put("充值方式", plusOrderEntity.getPayTypes() + "");
            map1.put("本次生效时间", dateFormat.format(plusOrderEntity.getStartTime()) + "");
            map1.put("本次到期时间", dateFormat.format(plusOrderEntity.getEndTime()) + "");
            map1.put("支付时间", dateFormat.format(plusOrderEntity.getCreatTime()) + "");
            map1.put("充值单号", plusOrderEntity.getPlusOrderid() + "");

            list.add(map1);
        }

        if(page.getRecords().size()>=30000){
            return ResponseResult.fail(BizCodeEnume.PLEASE_BE_LESS_THAN_30000_PIECES);
        }
        // excel文件名
        Map<String/* 此处的key为每个sheet的名称，一个excel中可能有多个sheet页 */, List<Map<String/* 此处key对应每一列的标题 */, String>>/*
         * 该list为每个sheet页的数据
         */> map2 = Maps
                .newHashMap();
        map2.put("测试合并数据", list);
        String url = ExcelsUtil.createExcel(title, map2, new int[]{0}/* 此处数组为需要合并的列，可能有的需求是只需要某些列里面相同内容合并 */);
        return ResponseResult.success(url);
    }



    @Override
    public ResponseResult list(Map<String, Object> map) {
        String userInfoStr = RedisUtil.get(YFConfig.YF_PLATFORM_TOKEN_USER + map.get("token"));
        if (null == userInfoStr || userInfoStr.equals("")) {
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_INVALID);
        }


        if (map.get("startTime") == ""){
            map.put("startTime",null);
        }
        if (map.get("endTime") == ""){
            map.put("endTime",null);
        }
        //查询会员充值列表
        IPage<Map<String,Object>> page=this.baseMapper.selectplusorderMap(new Query<Map<String,Object>>().getPage(map),
                map);

        //获取当前月份第一天和最后一天
        LocalDate date = LocalDate.now();
        // 获取当前月的第一天
        LocalDate firstDay = date.with(TemporalAdjusters.firstDayOfMonth());
        // 获取当前月的最后一天
        LocalDate lastDay = date.with(TemporalAdjusters.lastDayOfMonth());

        LocalDateTime startTime = firstDay.atTime(0, 0, 0);
        LocalDateTime endTime = lastDay.atTime(23, 59, 59);

        HashMap<String, Object> timemap = new HashMap<>();
        timemap.put("startTime",startTime);
        timemap.put("endTime",endTime);

        Map<String,Object> result=new HashMap<>();
        result=this.baseMapper.selectPriceTotal(timemap);
        result.put("page",PageUtils.getPage(page));

        return ResponseResult.success(result);
    }
}




